package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;  
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.List;

import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionSupport;

import entity.*;

public class StudentAction extends ActionSupport { 

	/**
	 * 
	 */
	private static final long serialVersionUID = 764122869845232420L;
	private Admin admin;
	private Courses courses;
	private Curriculum curriculum;
	private CurriculumId curriculumId;
	private Exam exam;
	private Examinationquestion examinationquestion;
	private ExaminationquestionId examinationquestionId;
	private Questionbank questionbank;
	private Score score;
	private ScoreId scoreId;
	private Students students;
	private Studentsanswer studentsanswer;
	private Teachers teachers;
	
	private AdminDAO adminDAO;
	private CoursesDAO coursesDAO;
	private CurriculumDAO curriculumDAO;
	private ExamDAO examDAO;
	private ExaminationquestionDAO examinationquestionDAO;
	private QuestionbankDAO questionbankDAO;
	private ScoreDAO scoreDAO;
	private StudentsDAO studentsDAO;
	private StudentsanswerDAO studentsanswerDAO;
	private TeachersDAO teachersDAO;
	
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public Courses getCourses() {
		return courses;
	}
	public void setCourses(Courses courses) {
		this.courses = courses;
	}
	public Curriculum getCurriculum() {
		return curriculum;
	}
	public void setCurriculum(Curriculum curriculum) {
		this.curriculum = curriculum;
	}
	public CurriculumId getCurriculumId() {
		return curriculumId;
	}
	public void setCurriculumId(CurriculumId curriculumId) {
		this.curriculumId = curriculumId;
	}
	public Exam getExam() {
		return exam;
	}
	public void setExam(Exam exam) {
		this.exam = exam;
	}
	public Examinationquestion getExaminationquestion() {
		return examinationquestion;
	}
	public void setExaminationquestion(Examinationquestion examinationquestion) {
		this.examinationquestion = examinationquestion;
	}
	public ExaminationquestionId getExaminationquestionId() {
		return examinationquestionId;
	}
	public void setExaminationquestionId(ExaminationquestionId examinationquestionId) {
		this.examinationquestionId = examinationquestionId;
	}
	public Questionbank getQuestionbank() {
		return questionbank;
	}
	public void setQuestionbank(Questionbank questionbank) {
		this.questionbank = questionbank;
	}
	public Score getScore() {
		return score;
	}
	public void setScore(Score score) {
		this.score = score;
	}
	public ScoreId getScoreId() {
		return scoreId;
	}
	public void setScoreId(ScoreId scoreId) {
		this.scoreId = scoreId;
	}
	public Students getStudents() {
		return students;
	}
	public void setStudents(Students students) {
		this.students = students;
	}
	public Studentsanswer getStudentsanswer() {
		return studentsanswer;
	}
	public void setStudentsanswer(Studentsanswer studentsanswer) {
		this.studentsanswer = studentsanswer;
	}
	public Teachers getTeachers() {
		return teachers;
	}
	public void setTeachers(Teachers teachers) {
		this.teachers = teachers;
	}
	public Session getSession() {
		return session;
	}
	public void setSession(Session session) {
		this.session = session;
	}
	public StudentAction(){
		
	}
	
	
	private Session session;
	private PrintWriter out;
	private JSONObject json;
	public void beginSession(){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");    
		session = HibernateSessionFactory.getSession();session.beginTransaction();
	}
	public void closeSession(){
		session.getTransaction().commit();
		session.close();
	}
	public void beginOut() throws IOException{
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");    
		out = ServletActionContext.getResponse().getWriter();
		json=new JSONObject();  
	}
	public void closeOut(){
		out.flush(); out.close();
	}
	
	public String logined() throws IOException{
		System.out.println("logined");
		String rs = (String)ServletActionContext.getContext().getSession().get("logined");
		beginOut();
		if ( rs==null ) out.println("1");
		else out.println("0");
		closeOut();
		return null;
	}
	public String login() throws IOException{
		String success = "loginSuccess";
		String fail = "loginFail";
		
		System.out.println("login");
		beginSession();
		studentsDAO = new StudentsDAO();
		Students rs = studentsDAO.findById( students.getStudentNo() );
		closeSession();
		
		if ( rs==null ) {
			ServletActionContext.getContext().getSession().put("logined","fail");
			return fail;
		}
		if ( !rs.getPassword().equals( students.getPassword()) ){
			ServletActionContext.getContext().getSession().put("logined","fail");
			return fail;
		}
		
		Object logined = ServletActionContext.getContext().getSession().get("logined");
		if(logined!=null) ServletActionContext.getContext().getSession().remove("logined");
		ServletActionContext.getContext().getSession().put("student",rs);
		return success;
	}
	
	public String getInfo() throws IOException{
		students = (Students)ServletActionContext.getContext().getSession().get("student");
		beginOut();
		json.accumulate("student", students);
		out.println(json);
		System.out.println(json);
		closeOut();
		return null;
	}
	public String changePassword() throws IOException{
		System.out.println("change password.");
		beginOut();
		
		json.accumulate("name", "ca");
		json.accumulate("password", "111111");
		
		//System.out.println(json.toString());
		out.print(json);
		
		closeOut();
		return null;
	}
	public String changeTele(){
		return null;
	}
		
	public String logout(){
		System.out.println("logout");
		ServletActionContext.getContext().getSession().remove("student");
		return "logout";
	}
} 
